package model

import (
	"gorm.io/gorm/clause"
	"server/common"
	"server/pkg/app"
)

type HisPipe struct {
	Model
	Status string `gorm:"column:status;not null;type:varchar(16);" json:"status"`

	OwnerID int `json:"owner_id"`
	Owner   Owner

	PipeLineID int `json:"pipeline_id"`
	PipeLine   Bpipe
}

func (hispipe *HisPipe) Create() error {
	if err := common.DB.Omit(clause.Associations).Create(&hispipe).Error; err != nil {
		return err
	}
	return nil
}

func (hispipe *HisPipe) TableName() string {
	return "hispipe"
}

func (hispipe *HisPipe) List(page *app.Page) ([]HisPipe, int64) {
	pipes := []HisPipe{}
	var count int64
	if err := common.DB.Preload(clause.Associations).Order("id desc").Limit(page.Size).Offset((page.Page - 1) * page.Size).Find(&pipes).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
		return pipes, count
	}
	return pipes, count
}

func (hispipe *HisPipe) Update() error {
	if err := common.DB.Where("id = ?", hispipe.ID).Updates(&hispipe).Error; err != nil {
		return err
	}
	return nil
}
